package com.lzkj.raos.weixin2second2demo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2021/3/23 11:29
 * 微信鉴权工具类
 */
public class AuthUtil {

    /** 公众号测试账号id和秘钥 */
    public static final String APPID = "wx41c6d1c7696ccea7";
    public static final String APPSECRET = "440a74b30a0338ee0002b975ffda783b";

    /**
     * 获取鉴权结果
     * @param url 请求地址
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static JSONObject doGetJson(String url) throws ParseException, IOException {
        JSONObject jsonObject = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            // 把返回的结果转换为JSON对象
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSON.parseObject(result);
        }
        return jsonObject;
    }

}
